package com.eyaomall.wxmall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.dao.GoodsDao;
import com.eyaomall.wxmall.dao.ShoppingChartDao;
import com.eyaomall.wxmall.model.po.Goods;
import com.eyaomall.wxmall.model.po.ShoppingChart;
import com.eyaomall.wxmall.model.vo.ShoppingChartVO;
import com.eyaomall.wxmall.service.ShoppingChartService;
import com.lycheeframework.core.cmp.kit.IHTTPObject;
import com.lycheeframework.core.cmp.kit.Pages;
import com.lycheeframework.core.exception.ErrorCodeException;
@Service
public class ShoppingChartServiceImpl implements ShoppingChartService {

	@Autowired
	private ShoppingChartDao shoppingChartDao;
	
	@Autowired
	private GoodsDao goodsDao;
	
	/**
	 * 分页查询我的购物车中的数据
	 */
	public Pages<List<ShoppingChartVO>> page(Integer memberId,IHTTPObject httpObject) {
		Pages<List<ShoppingChartVO>> page=shoppingChartDao.page(memberId, httpObject);
		if (page.getTotal()>0) {
			for (ShoppingChartVO vo : page.getRows()) {
				//校验已删除或者下线
				if("n".equalsIgnoreCase(vo.getIsPublish()) || "y".equalsIgnoreCase(vo.getGoodsDel())){
					vo.setInvalid(true);
					vo.setInvalidMsg("商品未上架");
				}
				//校验库存
				if(vo.getNum() > vo.getInventory()){
					vo.setInvalid(true);
					vo.setInvalidMsg("商品库存不足");
				}
			}
		}
		return page;
	}

	/**
	 * 查询我的购物车中的数量
	 */
	public Integer count(Integer memberId) {
		
		return shoppingChartDao.count(memberId);
	}

	/**
	 * 新增或者删除购物车产品信息
	 * @param goodsId
	 * @param num
	 * @return
	 */
	public Integer addOrUpdate(Integer goodsId,Integer num){
		ShoppingChart chart = new ShoppingChart();
		chart.setGoodsId(goodsId);
		Integer memberId = MemberHelper.getId();
		chart.setMemberId(memberId);
		chart = (ShoppingChart) shoppingChartDao.query(chart);
		
		Goods goods = goodsDao.query(goodsId);
		if(num > goods.getInventory()){
			throw new ErrorCodeException(BECode.shopping_chart_7000002);
		}
		
		if(chart == null){
			chart = new ShoppingChart();
			chart.setGoodsId(goodsId);
			chart.setNum(1);
			chart.setMemberId(memberId);
			shoppingChartDao.insert(chart);
			return chart.getId();
		}else{
			chart.setNum(num);
			shoppingChartDao.update(chart);
			
			return chart.getId();
		}
	}
	
	/**
	 * 根据ID删除购物车产品数量
	 * @param id
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean deleteById(String ids){
		String[] idArray = ids.split(",");
		for(String id : idArray){
			ShoppingChart chart = new ShoppingChart();
			chart.setIsDeleted("y");
			chart.setId(Integer.parseInt(id));
			
			shoppingChartDao.update(chart);
		}
		
		return  true;
	}
	
	/**
	 * 根据会员ID和商品ID查询购物车详情
	 * @param id
	 * @return
	 */
	public ShoppingChart query(Integer goodsId,Integer memberId){
		ShoppingChart chart = new ShoppingChart();
		chart.setGoodsId(goodsId);
		chart.setMemberId(memberId);
		
		return (ShoppingChart) shoppingChartDao.query(chart);
	}
}
